package net.phoenix.repository.value;

import java.util.Calendar;

import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;

import net.phoenix.repository.RepositoryRuntimeException;

/**
 * 
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-3
 * @version 1.0.0
 */
public class LongValue extends BasicValue {

	private long value;

	public LongValue(long value) {
		this.value = value;
	}

	public LongValue(String value) {
		if ((value == null) || (value.length() == 0))
			this.value = 0;
		else {
			try {
				this.value = Long.parseLong(value);
			} catch (NumberFormatException ex) {
				this.value = Math.round(Double.parseDouble(value));
			}
		}
	}

	/**
	 * getType
	 * 
	 * @return int
	 */
	public int getType() {
		return PropertyType.LONG;
	}

	public Calendar getDate() {
		Calendar date = Calendar.getInstance();
		date.setTimeInMillis(this.value);
		return date;
	}

	public void setValue(Calendar date) {
		if (date == null)
			this.value = 0;
		else
			this.value = date.getTimeInMillis();
	}

	public String getString() {
		return Long.toString(this.value);
	}

	public long getLong() {
		return this.value;
	}

	public double getDouble() {
		return this.value;
	}



	public boolean getBoolean() {
		return this.value != 0;
	}

	public int compareTo(Value value) {
		try {
			return (int) (this.value - value.getLong());
		} catch (RepositoryException e) {
			throw new RepositoryRuntimeException(e);
		}
	}

	public boolean equals(Value o) {
		if (this.compareTo((Value) o) == 0)
			return true;
		else
			return false;

	}
}
